﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
namespace StreetSmartAdmin
{
    public partial class Account : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                MultiView1.ActiveViewIndex = 0;
            }
        }

        protected void insertRecord(string username, string password)
        {
            string insertcommand = "insert into Users (UserName, Password) values('" + username + "' , '" + password + "');";
            string connectionString = null;
            System.Configuration.ConnectionStringSettings connString = System.Configuration.ConfigurationManager.ConnectionStrings["SQLServer2005DBConnectionString"];
            if (null != connString)
                connectionString = connString.ConnectionString;
            SqlConnection connection = null;
            connection = new SqlConnection(connectionString);
            connection.Open();
            SqlCommand command;
            command = connection.CreateCommand();
            command.CommandText = insertcommand;
            command.ExecuteNonQuery();
            connection.Close();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (!(TextBox1.Text == ""))
            {
                insertRecord(TextBox1.Text, TextBox2.Text);
                GridView1.DataBind();
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            if (validation(TextBox3.Text, TextBox4.Text))
            {
                MultiView1.ActiveViewIndex = 1;
            }
            else
            {
                MessageBox("Invalid login username or password!");
                MultiView1.ActiveViewIndex = 0;
            }
        }

        private void MessageBox(string message)
        {
            if (!string.IsNullOrEmpty(message))
            {
                this.ClientScript.RegisterClientScriptBlock(this.GetType(), "alt1", "alert('" + message + "');", true);
            }
        }

        private bool validation(string AdminName, string Password)
        {
            bool boolReturnValue = false;
            System.Configuration.ConnectionStringSettings connString = System.Configuration.ConfigurationManager.ConnectionStrings["SQLServer2005DBConnectionString"];
            string connectionString = connString.ConnectionString;
            SqlConnection Connection = new SqlConnection(connectionString);
            String strSQL = "Select * From Admin;";
            SqlCommand command = new SqlCommand(strSQL, Connection);
            SqlDataReader Dr;
            Connection.Open();
            Dr = command.ExecuteReader();
            while (Dr.Read())
            {
                if ((AdminName==(Dr["AdminName"].ToString())) && (Password==(Dr["Password"].ToString())))
                { 
                    boolReturnValue = true;
                    Dr.Close();
                    return boolReturnValue;
                }
            }
            return boolReturnValue;
        }

    }
}